package cn.itsource.order.controller;

import cn.itsource.basic.utils.AjaxResult;
import cn.itsource.basic.utils.PageBean;
import cn.itsource.basic.utils.SystemContext;
import cn.itsource.order.domain.ProductOrder;
import cn.itsource.order.query.ProductOrderQuery;
import cn.itsource.order.service.IProductOrderService;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.service.IShopService;
import cn.itsource.user.domain.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/productOrder")
public class ProductOrderController {

    @Autowired
    private IProductOrderService productOrderService;

    @Autowired
    private IShopService shopService;

    /**
     * 提交服务订单
     * @param param  addressId: 1,payType: 3,products: [{id: 16, num: 1}, {id: 66, num: 2}]
     * @param request
     * @return
     */
    @PostMapping("/submitOrder")
    public AjaxResult submitOrder(@RequestBody Map<String,Object> param, HttpServletRequest request){
        try {
            LoginInfo loginInfo = SystemContext.getLoginInfo(request);
            String result = productOrderService.submitOrder(param, loginInfo);
            return AjaxResult.me().setResultObj(result);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

    @PostMapping
    public PageBean<ProductOrder> queryPage(@RequestBody ProductOrderQuery query, HttpServletRequest request){
        LoginInfo loginInfo = SystemContext.getLoginInfo(request);
        Shop shop = shopService.loadByLogininfoid(loginInfo.getId());
        query.setShopId(shop.getId());

        return  productOrderService.queryPage(query);
    }
}
